package com.igeek.collateral.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.collateral.constant.MessageConstant;
import com.igeek.collateral.entity.*;
import com.igeek.collateral.result.Result;
import com.igeek.collateral.service.IAgencyService;
import com.igeek.collateral.vo.InOutNumberVo;
import com.igeek.collateral.vo.PageQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/admin/agency")
@Api(tags = "机构接口")
@Slf4j
public class AgencyController {
    @Autowired
    private IAgencyService iAgencyService;
    @GetMapping("findPage")
    @ApiOperation("查询评估机构")
    public Result findPage(PageQueryVO vo){
        try {
            Page<Agency> page = iAgencyService.findPage(vo);
            if(page!=null){
                return new Result(true , MessageConstant.QUERY_AGENCY_SUCCESS, page);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false , MessageConstant.QUERY_AGENCY_FAIL);
    }

    @ApiOperation("新增评估机构")
    @PostMapping("add")
    public Result add(@ApiParam("评估机构") @RequestBody Agency agency) {
        try {
            iAgencyService.save(agency);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_AGENCY_FAIL);
        }
        return new Result(true, MessageConstant.ADD_AGENCY_SUCCESS);
    }
    @PutMapping("update")
    @ApiOperation("编辑评估机构")
    public Result update(@RequestBody Agency agency){
        try {
            boolean flag = iAgencyService.updateById(agency);
            if(flag){
                return new Result(true , MessageConstant.EDIT_AGENCY_SUCCESS);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false , MessageConstant.EDIT_AGENCY_FAIL);
    }
    @GetMapping("{id}")
    @ApiOperation("根据id查询机构信息")
    public Result findById(@PathVariable("id") Long id){
        try {
            Agency agency = iAgencyService.getById(id);
            if(agency!=null){
                return new Result(true , MessageConstant.QUERY_AGENCY_SUCCESS, agency);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false , MessageConstant.QUERY_AGENCY_FAIL);
    }
    @DeleteMapping("{id}")
    @ApiOperation("根据id删除评估机构")
    public Result delete (@PathVariable("id") Long id)
    {
        try {
            boolean flag=iAgencyService.delete(id);
            if(flag)
            {
                return new Result(true,MessageConstant.DELETE_AGENCY_SUCCESS);
            }
        } catch (RuntimeException e) {
            return new Result(false,e.getMessage());
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.DELETE_AGENCY_FAIL);
    }
    @GetMapping("selectAll/{id}")
    @ApiOperation("根据id查询机构以及评估报告信息")
    public Result selectALL(@PathVariable("id") Long id){
        try {
            Agency agency = iAgencyService.selectAll(id);
            if(agency!=null){
                return new Result(true , MessageConstant.QUERY_AGENCY_SUCCESS,agency);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false , MessageConstant.QUERY_AGENCY_FAIL);
    }
    @DeleteMapping ("deleteAll/{id}")
    @ApiOperation("根据id删除机构以及评估报告信息")
    public Result deleteAll(@PathVariable("id") Long id){
        try {
            boolean flag = iAgencyService.deleteAll(id);
            if(flag){
                return new Result(true , MessageConstant.DELETE_AGENCY_SUCCESS);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false , MessageConstant.DELETE_AGENCY_FAIL);
    }

//    @PostMapping("test")
//    public  Result testNumber(String numKey, long count)
//    {
//        long a=iAgencyService.testNumber(numKey,count);
//        System.out.println(a);
//        if(a>=0l)
//        {
//            return new Result(true,"成功",a);
//        }
//        return new Result(false,"失败");
//    }


    //获取数据

}
